//
//  FFWallItemListCell.m
//  LLWallet
//
//  Created by tt on 2020/8/16.
//

#import "FFWallItemListCell.h"
@implementation FFWallItemModel

+(FFWallItemModel*) initWithTitle:(NSString*)title icon:(UIImage*)icon onClick:(LIMWallItemClick)onClick {
    FFWallItemModel *model = [FFWallItemModel new];
    model.title = title;
    model.icon = icon;
    model.onClick = onClick;
    return model;
}

@end

@implementation FFWallItemListModel

- (Class)cell {
    return FFWallItemListCell.class;
}

@end

@interface FFWallItemListCell ()

@property(nonatomic,strong) UIView *boxView;
@property(nonatomic,strong) FFWallItemListModel *model;

@end
#define eachLineNum 3 // 每行item数量
#define eachItemheight 60.0f
@implementation FFWallItemListCell

+ (CGSize)sizeForModel:(FFWallItemListModel *)model {
    CGFloat height = 0.0f;
    if(model.items && model.items.count>0) {
       CGFloat count = model.items.count/3;
        if(model.items.count%3 !=0) {
            count = count + 1;
        }
        height = eachItemheight * count;
    }

    return CGSizeMake(WKScreenWidth, height);
}

- (void)setupUI {
    [super setupUI];
    [self setBackgroundColor:[UIColor clearColor]];
    [self.contentView addSubview:self.boxView];
}
- (UIView *)boxView {
    if(!_boxView) {
        _boxView = [[UIView alloc] init];
        [_boxView setBackgroundColor:[UIColor clearColor]];
    }
    return _boxView;
}


- (void)refresh:(FFWallItemListModel *)model {
    [super refresh:model];
    self.model = model;
    
    [self.boxView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    if(model.items && model.items.count>0) {
        for (NSInteger i=0; i<model.items.count; i++) {
            FFWallItemModel *item = model.items[i];
            FFWallItemView *itemView = [[FFWallItemView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, eachItemheight)];
            itemView.titleLbl.text = item.title;
            [itemView.titleLbl sizeToFit];
            itemView.iconImgView.image = item.icon;
            [self.boxView addSubview:itemView];
            itemView.tag = i;
            [itemView addTarget:self action:@selector(itemCLick:) forControlEvents:UIControlEventTouchUpInside];
        }
        
    }
}

-(void) itemCLick:(FFWallItemView*)view {
    NSInteger index = view.tag;
   FFWallItemModel *itemModel = self.model.items[index];
    if(itemModel.onClick) {
        itemModel.onClick();
    }
}


- (void)layoutSubviews {
    [super layoutSubviews];
    self.boxView.frame = self.bounds;
    NSArray *itemViews = [self.boxView subviews];
    if(itemViews) {
        CGFloat itemWidth = self.lim_width/3.0f;
        NSInteger lineIndex = -1;
        NSInteger lineStart = 0;
        for (NSInteger i=0; i<itemViews.count; i++) {
            lineStart++;
            if( i%3 == 0) {
                lineIndex++;
                lineStart = 0;
            }
            
            UIView *itemView = itemViews[i];
            itemView.lim_width = itemWidth;
            itemView.lim_left = lineStart*itemWidth;
            itemView.lim_top = eachItemheight * lineIndex;
            
            
        }
    }
}

@end



@implementation FFWallItemView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.titleLbl];
        [self addSubview:self.iconImgView];
    }
    return self;
}

- (UIImageView *)iconImgView {
    if(!_iconImgView) {
        _iconImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 30.0f, 30.0f)];
    }
    return _iconImgView;
}

- (UILabel *)titleLbl {
    if(!_titleLbl) {
        _titleLbl = [[UILabel alloc] init];
        [_titleLbl setFont:[[WKApp shared].config appFontOfSize:14.0f]];
        [_titleLbl setTextColor:[WKApp shared].config.defaultTextColor];
    }
    return _titleLbl;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.iconImgView.lim_top = 4.0f;
    self.iconImgView.lim_left = self.lim_width/2.0f - self.iconImgView.lim_width/2.0f;
    
    self.titleLbl.lim_left = self.lim_width/2.0f - self.titleLbl.lim_width/2.0f;
    self.titleLbl.lim_top = self.iconImgView.lim_bottom + 2.0f;
}

@end
